package com.atguigu.coupon.service.impl;

import com.atguigu.coupon.entity.SeckillSkuRelationEntity;
import com.atguigu.coupon.service.SeckillSkuRelationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.coupon.dao.SeckillSessionDao;
import com.atguigu.coupon.entity.SeckillSessionEntity;
import com.atguigu.coupon.service.SeckillSessionService;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    private SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> getLates3DaySession() {

        //计算最近三天
        //查出这三天参与秒杀活动的商品
        List<SeckillSessionEntity> seckillSessionEntities = this.list(
                new LambdaQueryWrapper<SeckillSessionEntity>()
                        .between(SeckillSessionEntity::getStartTime, getStartTime(), getEndTime())

        );
        if (seckillSessionEntities!=null && seckillSessionEntities.size()>0) {

            List<SeckillSessionEntity> collect = seckillSessionEntities.stream()
                    .map(sessionEntity -> {
                        //查出sms_seckill_sku_relation关联的商品(sku)
                        List<SeckillSkuRelationEntity> skuRelationEntityList = seckillSkuRelationService.list(
                                new LambdaQueryWrapper<SeckillSkuRelationEntity>()
                                        .eq(SeckillSkuRelationEntity::getPromotionSessionId, sessionEntity.getId())
                        );
                        sessionEntity.setRelations(skuRelationEntityList);
                        return sessionEntity;
            }).collect(Collectors.toList());

            return collect;
        }


        return null;
    }

    //当前日期 00:00:00
    private String getStartTime(){
        LocalDate now = LocalDate.now();
        LocalTime min = LocalTime.MIN;
        LocalDateTime startTime = LocalDateTime.of(now, min);
        String format = startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }

    //当前日期 23:59:59
    private String getEndTime(){
        LocalDate now = LocalDate.now();
        LocalDateTime endTime = now.plusDays(2L).atTime(LocalTime.MAX);
        String format = endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }


}